//
//  DiscoverViewController.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/4/19.
//

#import "DiscoverViewController.h"
#import "DiscoverBasicViewController.h"
#import "DiscoverExploreViewController.h"

@interface DiscoverViewController ()

@property(nonatomic,strong)UIButton *basicsBtn;
@property(nonatomic,strong)UIButton *questionBtn;
@property(nonatomic,strong)UIImageView *basicImageView;
@property(nonatomic,strong)UIImageView *questionImageView;
@property(nonatomic,strong)UIScrollView *bgScrollView;
@property(nonatomic,strong)DiscoverBasicViewController *basicVC;
@property(nonatomic,strong)DiscoverExploreViewController *exploreVC;


@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    self.customNavigationBar.backgroundColor = kTopBgColor(self.currentAvailableWidth,kYM_NAVIGATIONBAR_HEIGHT+1);
    self.view.backgroundColor = [UIColor whiteColor];
   
    self.basicImageView = [[UIImageView alloc]init];
    self.basicImageView.image = [UIImage imageNamed:@"discover_question_title_bottom"];
    self.basicImageView.userInteractionEnabled = YES;
    [self.customNavigationBar addSubview:self.basicImageView];
    [self.basicImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self_weak_.customNavigationBar).offset(-34);
        make.bottom.mas_equalTo(@-10);
        make.height.mas_equalTo(@10);
        make.width.mas_equalTo(@36);
    }];
    self.basicsBtn = [[UIButton alloc]init];
    [self.basicsBtn setTitle:@"基础" forState:UIControlStateNormal];
    [self.basicsBtn setTitleColor:KFontBlackColor forState:UIControlStateSelected];
    [self.basicsBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    self.basicsBtn.titleLabel.font = JPUIFontPingFangSCMedium(18);
    self.basicsBtn.backgroundColor = [UIColor clearColor];
    self.basicsBtn.selected = YES;
    [self.basicsBtn addTarget:self action:@selector(basicsBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.customNavigationBar addSubview:self.basicsBtn];
    [self.basicsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@46);
        make.height.mas_equalTo(@33);
        make.bottom.mas_equalTo(@-6);
        make.centerX.mas_equalTo(self_weak_.basicImageView);
    }];
    
    self.questionImageView = [[UIImageView alloc]init];
    self.questionImageView.image = [UIImage imageNamed:@"discover_question_title_bottom"];
    self.questionImageView.userInteractionEnabled = YES;
    [self.customNavigationBar addSubview:self.questionImageView];
    [self.questionImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self_weak_.customNavigationBar).offset(34);
        make.bottom.mas_equalTo(@-10);
        make.height.mas_equalTo(@10);
        make.width.mas_equalTo(@36);
    }];
    self.questionImageView.hidden = YES;
    
    self.questionBtn = [[UIButton alloc]init];
    [self.questionBtn setTitle:@"探索" forState:UIControlStateNormal];
    [self.questionBtn setTitleColor:KFontBlackColor forState:UIControlStateSelected];
    [self.questionBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    self.questionBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    self.questionBtn.backgroundColor = [UIColor clearColor];
    self.questionBtn.selected = NO;
    [self.questionBtn addTarget:self action:@selector(questionBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.customNavigationBar addSubview:self.questionBtn];
    [self.questionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@46);
        make.height.mas_equalTo(@33);
        make.bottom.mas_equalTo(@-6);
        make.centerX.mas_equalTo(self_weak_.questionImageView);
    }];
    
    self.bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,kYM_NAVIGATIONBAR_HEIGHT,self.currentAvailableWidth,self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-kYM_TABBAR_HEIGHT)];
    self.bgScrollView.scrollEnabled = NO;
    self.bgScrollView.pagingEnabled = YES;
    self.bgScrollView.backgroundColor = [UIColor whiteColor];
    self.bgScrollView.contentSize = CGSizeMake(2*self.currentAvailableWidth,0);
    [self.view addSubview:self.bgScrollView];
    
    self.basicVC = [[DiscoverBasicViewController alloc]init];
    self.basicVC.view.frame = CGRectMake(0,0,self.currentAvailableWidth,self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-kYM_TABBAR_HEIGHT);
    [self.bgScrollView addSubview:self.basicVC.view];
    [self addChildViewController:self.basicVC];
    
    
    self.exploreVC = [[DiscoverExploreViewController alloc]init];
    self.exploreVC.view.frame = CGRectMake(self.currentAvailableWidth,0,self.currentAvailableWidth,self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-kYM_TABBAR_HEIGHT);
    [self.bgScrollView addSubview:self.exploreVC.view];
    [self addChildViewController:self.exploreVC];
    
}

#pragma mark - 基础知识
-(void)basicsBtnClick{
    self.basicsBtn.selected = YES;
    self.basicImageView.hidden = NO;
    self.questionBtn.selected = NO;
    self.questionImageView.hidden = YES;
    self.basicsBtn.titleLabel.font = JPUIFontPingFangSCMedium(18);
    self.questionBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [self.bgScrollView setContentOffset:CGPointMake(0,0) animated:NO];
}

#pragma mark - 问答
-(void)questionBtnClick{
    
    self.basicsBtn.selected = NO;
    self.basicImageView.hidden = YES;
    self.questionBtn.selected = YES;
    self.questionImageView.hidden = NO;
    self.questionBtn.titleLabel.font = JPUIFontPingFangSCMedium(18);
    self.basicsBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [self.bgScrollView setContentOffset:CGPointMake(self.currentAvailableWidth,0) animated:NO];

}

#pragma mark - 屏幕宽度改变
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    if(isIPAD){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 获取当前可用宽度并更新布局
            self.currentAvailableWidth = [DeviceUtils currentAvailableWidth];
            self.currentAvailableHeight = [DeviceUtils currentAvailableHeight];
            self.customNavigationBar.frame = CGRectMake(0,0,self.currentAvailableWidth,kYM_NAVIGATIONBAR_HEIGHT);
            self.customNavigationBar.backgroundColor = kTopBgColor(self.currentAvailableWidth,kYM_NAVIGATIONBAR_HEIGHT+1);
            self.bgScrollView.frame = CGRectMake(0,kYM_NAVIGATIONBAR_HEIGHT,self.currentAvailableWidth,self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-kYM_TABBAR_HEIGHT);
            self.bgScrollView.contentSize = CGSizeMake(2*self.currentAvailableWidth,0);
            self.basicVC.view.frame = CGRectMake(0,0,self.currentAvailableWidth,self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-kYM_TABBAR_HEIGHT);
            self.exploreVC.view.frame = CGRectMake(self.currentAvailableWidth,0,self.currentAvailableWidth,self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-kYM_TABBAR_HEIGHT);
            if(self.questionBtn.selected == YES){
                [self.bgScrollView setContentOffset:CGPointMake(self.currentAvailableWidth,0) animated:NO];
            }
        });
    }
}


@end
